package com.qjl.shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjl.shopping.bean.Item;
import com.qjl.shopping.bean.Orders;
import com.qjl.shopping.bean.ShopCar;
import com.qjl.shopping.common.ResponseEntity;
import com.qjl.shopping.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author liguanghui
 * @Date 2023/5/3
 */
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @GetMapping("/find/order/{id}")
    public ResponseEntity findById(@PathVariable int id){
        Orders byId = ordersService.getById(id);
        if(byId==null){
            return ResponseEntity.error("查询失败，确认id是否有效");
        }
        return ResponseEntity.success("根据id查询成功",byId);
    }

    //查询全部
    @GetMapping("/find/all/orders")
    public ResponseEntity findAll(@RequestParam(defaultValue = "0") int pageNum,
                                  @RequestParam(defaultValue = "9") int pageSize,
                                  @RequestParam(defaultValue = "0") int userId){
        Page<Orders> page = ordersService.selectPage(pageNum, pageSize,userId);
        return ResponseEntity.success("查询全部order成功",page);
    }

    @GetMapping("/delete/order")
    public ResponseEntity delete(int id){
        if(ordersService.removeById(id)){
            return new ResponseEntity(200,"删除成功");
        }else {
            return new ResponseEntity(404,"删除失败");
        }
    }
    @PostMapping("/add/order")
    public ResponseEntity add(@RequestBody Orders orders){
        if(ordersService.save(orders)){
            return new ResponseEntity(200,"增加成功");
        }else {
            return new ResponseEntity(404,"增加失败");
        }
    }
    @PostMapping("/update/order")
    public ResponseEntity update(@RequestBody Orders orders){
        if(ordersService.updateById(orders)){
            return new ResponseEntity(200,"修改成功");
        }else {
            return new ResponseEntity(404,"修改失败");
        }
    }
}
